package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    // 处理新增请求 http://localhost/department/saveOrUpdate.do POST  name: adf sn: asdf
    @RequestMapping("/saveOrUpdate")
//    @RequiredPermission(name = "部门新增/编辑",expression = "department:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        if (department.getId() != null) { // 修改
            departmentService.update(department);
        }else { // 新增
            departmentService.save(department);
        }
        return new JsonResult();
    }

    // 处理去新增请求 http://localhost/department/input.do
   /* @RequestMapping("/input")
    public String input(Long id, Model model){
        // 若传 id 值，表示是去修改，那就要去查询被修改的部门对象，存在模型中
        if (id != null) {
            model.addAttribute("department", departmentService.get(id));
        }
        return "department/input"; // /WEB-INF/views/department/input.ftl
    }*/

    
    // 处理删除部门请求 http://localhost/department/delete.do?id=1
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"department:delete","部门删除"},logical = Logical.OR)
//    @RequiredPermission(name = "部门删除",expression = "department:delete")
    public JsonResult delete(Long id) {
        if (id != null) { // 传id值就删除对应的部门数据
            departmentService.delete(id);
        }
        return new JsonResult();
    }


    // 处理分页查询部门的请求
//    @RequiresRoles("admin")
    //允许配置多个权限表达式，默认的判断的逻辑就是与（两个都必须要有）
    //为了方便获取权限的名称，可以把逻辑改为或or (其中一个有就可能访问)
    @RequiresPermissions(value = {"department:list","部门页面"},logical = Logical.OR)
    @RequestMapping("/list")
//    @RequiredPermission(name = "部门页面",expression = "department:list")
    public String list(Model model, QueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        //需要判断角色或者权限的时候，shiro会自动调用realm的doGetAuthorizationInfo方法，获取到用户的数据，进行比对
        System.out.println("admin:"+subject.hasRole("admin"));
        System.out.println("employee:delete:"+subject.isPermitted("employee:delete"));

//        int i = 1/0;
        // 分页查询部门数据，存在模型
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        // 找视图
        return "department/list"; // /WEB-INF/views/department/potentialList.ftl
    }

}
